var express = require('express');
var router = express.Router();
var md5 = require('./../tools/md5.js')

var db = require('../sqldb/index.js');
var sysUser =require('../model/sysUser.js')
var campus =require('../model/campus.js')
var department =require('../model/department.js')

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

router.get('/add',function(req,res,next){
    console.log("+++++++++++++++++++++++");
    var newpassword=md5('123456')
    var saveUser = {
        studentNumber:'2015213741',
        password:newpassword,
        realName:'亮亮',
        department:'技术部'
    };

    return db.sequelize.transaction(function(t){
        console.log("+++++++++++++++++++");
        return sysUser(db.sequelize).create(saveUser,{
            transaction:t
        }).then(function(data){
            res.json({
                status:1,
                msg:'',
                result:data,
                hasLogin:1,
                identity:2
            })
        }).catch(function(err){
            console.log("发生错误：" + err);
        });
    })
});

router.post('/information', function(req, res, next) {
    return db.sequelize.transaction(function(t){
        console.log("+++++++++++++++++++")
        return sysUser(db.sequelize).findOne({
            where:{
                studentNumber:2015213740,
            }
        },{
            transaction:t
        }).then(function(data){
            res.json({
                status:1,
                msg:'',
                result:data,
                hasLogin:1,
                identity:5
            }) 
        }).catch(function(err){
            res.json({
                status:0,
                msg:err,
                result:'',
                hasLogin:0,
                identity:0
            })
        });
    })
});

router.post('/campus', function(req, res, next) {
    return db.sequelize.transaction(function(t){
        console.log("+++++++++++++++++++")
        return campus(db.sequelize).findAll({
          attributes: ['campusName']
        },{
            transaction:t
        }).then(function(data){
            res.json({
                status:1,
                msg:'',
                result:data,
                hasLogin:1,
                identity:5
            }) 
        }).catch(function(err){
            res.json({
                status:0,
                msg:err,
                result:'',
                hasLogin:0,
                identity:0
            })
        });
    })
});

router.post('/department', function(req, res, next) {
    return db.sequelize.transaction(function(t){
        console.log("+++++++++++++++++++")
        return department(db.sequelize).findAll({
          attributes: ['name']
        },{
            transaction:t
        }).then(function(data){
            res.json({
                status:1,
                msg:'',
                result:data,
                hasLogin:1,
                identity:5
            }) 
        }).catch(function(err){
            res.json({
                status:0,
                msg:err,
                result:'',
                hasLogin:0,
                identity:0
            })
        });
    })
});


module.exports = router;
